package com.neusoft.neuiotms.dcam.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.neuiotms.dcam.common.converter.PrinterConverter;
import com.neusoft.neuiotms.dcam.common.dto.PrinterDTO;
import com.neusoft.neuiotms.dcam.common.model.Printer;
import com.neusoft.neuiotms.dcam.common.repository.PrinterRepository;
import com.neusoft.neuiotms.dcam.common.service.PrinterService;

@Service
public class PrinterServiceImpl implements PrinterService {
    @Autowired
    private PrinterRepository printerRepository;
    @Autowired
    private PrinterConverter printerConverter;

    @Override
    public PrinterDTO setPrinterConfig(PrinterDTO dto) {
        Printer model = this.printerConverter.toModel(dto);
        if (model.isNew()) {
            final Printer p = this.printerRepository.findByDepartmentCode(dto.getDepartmentCode());
            if (null != p) {
                return this.printerConverter.toDTO(p);
            }
        }
        model = this.printerRepository.save(model);
        return this.printerConverter.toDTO(model);
    }

    @Override
    public List<PrinterDTO> getPrinterConfig(String belongs) {
        final List<Printer> p = this.printerRepository.findByBelongs(belongs);
        return this.printerConverter.toListDTO(p);
    }

    @Override
    public PrinterDTO getMyPrinter(String departmentCode) {
        final Printer p = this.printerRepository.findByDepartmentCode(departmentCode);
        if (null == p) {
            return null;
        }
        return this.printerConverter.toDTO(p);
    }

    @Override
    public void delete(final Long id) {
        this.printerRepository.deleteById(id);
    }
}
